//
//  UIPanel.cpp
//  storm
//
//  Created by wangdi on 13-8-20.
//
//

#include "UIPanel.h"

using namespace cocos2d;
namespace storm
{
    UIPanel::UIPanel()
        : m_rcClip(CCRectZero)
    {
        
    }
    
    UIPanel::~UIPanel()
    {
        
    }
    
    void UIPanel::visit()
    {
        if(m_rcClip.equals(CCRectZero))
        {
            CCLayer::visit();
            return;
        }
        
        
        glEnable(GL_SCISSOR_TEST);
        
        // 像素
        
        //    glScissor((m_rcShow.origin.x *0.5 *(m_parentScale + 1)+ getPositionX()* m_parentScale/m_scale)  , (m_rcShow.origin.y * 0.5 *(m_parentScale + 1)+ getPositionY()* m_parentScale/m_scale),
        //              m_rcShow.size.width* m_parentScale, m_rcShow.size.height* m_parentScale);
        // 此区域永远相对于屏幕，并且以像素为单位
        CCNode *pParent = getParent();
        if(NULL != pParent)
        {
            CCPoint pt = pParent->convertToWorldSpace(getPosition());
            float scale = 1.0f;
            while (pParent != NULL) {
                scale *= pParent->getScale();
                pParent = pParent->getParent();
            }
            glScissor(m_rcClip.origin.x * scale+ pt.x,
                      m_rcClip.origin.y * scale + pt.y,
                      m_rcClip.size.width* scale,
                      m_rcClip.size.height* scale);

        }
               
        UIPanel::visit();
        glDisable(GL_SCISSOR_TEST);
    }
    void UIPanel::setEnable(bool bEnable)
    {
        UIWidget::setEnable(bEnable);
        
        for(WidgetList::iterator iter = m_listWidgets.begin(); iter != m_listWidgets.end(); iter++)
        {
            (*iter)->setEnable(bEnable);
        }
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIPanel::loseFocus()
    {
        UIWidget::loseFocus();
        
        for(WidgetList::iterator iter = m_listWidgets.begin(); iter != m_listWidgets.end(); iter++)
        {
            (*iter)->loseFocus();
        }
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIPanel::fadeOut(float duration)
    {
        for(WidgetList::iterator iter = m_listWidgets.begin(); iter != m_listWidgets.end(); iter++)
        {
            (*iter)->fadeOut(duration);
        }
        
        UIWidget::fadeOut(duration);
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIPanel::fadeIn(float duration)
    {
        for(WidgetList::iterator iter = m_listWidgets.begin(); iter != m_listWidgets.end(); iter++)
        {
            (*iter)->fadeIn(duration);
        }
        
        UIWidget::fadeIn(duration);
    }
    
    UIWidget *UIPanel::onReceiveNewTouch(CCTouch *pTouch)
    {
        UIWidget *pRet = NULL;
        for(WidgetList::iterator iter = m_listWidgets.begin(); iter != m_listWidgets.end(); iter++)
        {
            if(NULL != (pRet = (*iter)->onReceiveNewTouch(pTouch)))
            {
                return pRet;
            }
        }
        
        return pRet;
    }

    
    void UIPanel::AddChildWidget(storm::UIWidget *pChild)
    {
        addChild(pChild);
        m_listWidgets.push_back(pChild);
    }
}